package com.zijiebutiao.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijiebutiao.pojos.*;
import com.zijiebutiao.service.TMemberService;
import com.zijiebutiao.utils.ResultVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Objects;

/**
 * @Date 2023/3/7 9:57
 * @Author 干饭
 * @Email 1229219472@qq.com
 * @Version v1.0
 */
@RestController
@RequestMapping("/tmember")
@Api(tags = "会员档案")
public class TMemberController {
    @Autowired
    private TMemberService tMemberService;

    /**
     * 分页查询
     * @param param 通过条件查询
     * @return  ResultVo返回对象data和code错误，errorMsg错误信息
     */
    @PostMapping("/findPage.do")
    public ResultVo getAll(@RequestBody Param param) {
        ResultVo resultVo = new ResultVo();
        Integer currentPage = Integer.valueOf(param.getCurrentPage());
        Integer pageSize = Integer.valueOf(param.getPageSize());
        System.out.println(param);
        if (("").equals(currentPage) || currentPage == 0) {
            currentPage = 1;
        }
        if (("").equals(pageSize) || pageSize == 0) {
            pageSize = 10;
        }
        if (Objects.isNull(param.getQueryString())) {
            IPage<TMember> page = new Page<>(currentPage, pageSize);
            resultVo.setData(tMemberService.page(page));
            return resultVo;
        } else {
            IPage<TMember> page = new Page<>(currentPage, pageSize);
            Wrapper<TMember> qw = Wrappers.lambdaQuery(TMember.class)
                    .eq(TMember::getFilenumber, param.getQueryString())
                    .or().eq(TMember::getName, param.getQueryString()).or()
                    .eq(TMember::getPhonenumber,param.getQueryString());
            TMember one = tMemberService.getOne(qw);
            long count = tMemberService.count(qw);
            System.out.println(count);
            System.out.println(count);
            if (count < 1) {
                IPage<TMember> page1 = new Page<>(currentPage, pageSize);
                resultVo.setData(tMemberService.page(page1));
                resultVo.setCode(403);
                resultVo.setErrorMsg("项目编码或项目名称不存在");
                return resultVo;
            }
            page.setRecords(Arrays.asList(one));
            page.setCurrent(count);
            page.setSize(pageSize);
            page.setPages(currentPage);
            page.setTotal(count);

            resultVo.setData(page);
        }
        return resultVo;
    }

    @Transactional
    @PostMapping("/edit.do")
    public ResultVo updateTMember(@RequestBody TMember tMember) {
        ResultVo resultVo = new ResultVo();
        TMember tCheckitem = tMemberService.getById(tMember.getId());
        Wrapper<TMember> wq = Wrappers.lambdaQuery(TMember.class)
                .ne(TMember::getFilenumber, tCheckitem.getFilenumber());
        for (TMember checkitem : tMemberService.list(wq)) {
            if (checkitem.getFilenumber().equals(tMember.getFilenumber())) {
                resultVo.setCode(403);
                resultVo.setErrorMsg("编码重复修改失败");
                return resultVo;
            }
        }
        Wrapper<TMember> qw = Wrappers.lambdaQuery(TMember.class)
                .eq(TMember::getId, tMember.getId());
        tMemberService.update(tMember, qw);
        resultVo.setErrorMsg("修改成功");
        return resultVo;
    }

    @Transactional
    @PostMapping("/add.do")
    public ResultVo addTMember(@RequestBody TMember tMember) {
        ResultVo resultVo = new ResultVo();
        LambdaQueryWrapper<TMember> qw = new LambdaQueryWrapper<>();
        qw.eq(TMember::getFilenumber,tMember.getFilenumber());
        TMember byId = tMemberService.getOne(qw, false);
        System.out.println(byId);
        if (byId == null) {
            tMemberService.save(tMember);
            resultVo.setCode(200);
            resultVo.setErrorMsg("添加成功");
        } else {
            resultVo.setErrorMsg("项目编码已存在，不能重复");
            resultVo.setCode(403);
        }
        return resultVo;
    }

    @Transactional
    @GetMapping("/delete.do")
    public ResultVo delete(String id) {
        ResultVo resultVo = new ResultVo();
        TMember byId = tMemberService.getById(id);
        if (byId!=null) {
            tMemberService.removeById(id);
            resultVo.setErrorMsg("删除成功");
        }else {
            resultVo.setErrorMsg("删除失败");
            resultVo.setCode(403);
        }
        return resultVo;
    }

}
